


class WalkCommand extends command.Command {

    private x: number;
    private y: number;

    constructor(x: number, y: number) {
        super();
        this.x = x;
        this.y = y;
    }

    execute(notifyFinish: Function) {

        console.log(`开始走路！！！从${this.x}出发`);
        const map = mapManager.getMap();
        map.grid.setEndNode(this.x, this.y);
        const findpath = new astar.AStar();
        findpath.setHeurisitic(findpath.diagonal)
        const result = findpath.findPath(map.grid);

        const path = findpath._path;

        if (path) {
            path.shift();
            user.goto(path);

            const func = () => {
                user.removeEventListener("goOver", func);
                notifyFinish();
            }
            user.addEventListener("goOver", func)
        }
    }
}

class PickCommand extends command.Command {


    private weapon: Tool;
    constructor(weapon: Tool) {
        super();
        this.weapon = weapon;
    }

    execute(notifyFinish: Function) {
        const map = mapManager.getMap();
        user.pick(this.weapon)
        user.level++;
        map.removeItem(this.weapon);

        notifyFinish();
    }
}

class TalkCommand extends command.Command {

    private npc: Npc;
    constructor(npc: Npc) {
        super();
        this.npc = npc;
    }


    execute(notifyFinish: Function) {
        console.log(`开始对话:${this.npc.toString()}`)
        let mission: Mission | null = null;
        console.log(this.npc.id);

        if (this.npc.canSubmitMissions.length > 0) {
            mission = this.npc.canSubmitMissions[0];
        }
        if (this.npc.canAcceptMissions.length > 0) {
            mission = this.npc.canAcceptMissions[0];
        }
        else {

        }
        if (mission) {
            const dialog = new TalkDialog(this.npc);
            dialogContainer.addChild(dialog);
            dialog.setMission(mission, this.npc);
            dialog.addEventListener('close', () => {
                dialogContainer.removeChild(dialog)
                if (mission) {
                    if (mission.status == MissionStatus.CAN_SUBMIT) {
                        console.log('提交任务', mission.name)
                        missionManager.submitMission(mission);
                    }
                    else if (mission.status == MissionStatus.CAN_ACCEPT) {
                        console.log('接受任务', mission.name)
                        missionManager.acceptMission(mission);
                    }
                }
                notifyFinish();

            })

        }
        else {
            notifyFinish();
        }






    }
}
class GotoNextMapCommand extends command.Command {

    constructor(private x: number, private y: number, private mapId: number) {
        super();
    }

    execute(notifyFinish: Function) {

        console.log('换地图！！')
        mapManager.changeMap(this.x, this.y, this.mapId)
        notifyFinish();
    }
}

class GameAsideCommand extends command.Command {
    constructor(private gameasideId: number) {
        super();
    }

    execute(notifyFinish: Function) {
        const gameaside = gameAsideManager.getAsideById(this.gameasideId);
        const gameasidedialog = new AsideDialog(gameaside);
        dialogContainer.addChild(gameasidedialog);
        gameasidedialog.addEventListener('close', () => {
            dialogContainer.removeChild(gameasidedialog)
            notifyFinish();
        })

    }
}


class CheckLetterCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const image = new Image();
        image.src = "assets/tool/letter.png";
        const Bitmap = new renderer.Bitmap(160, 96, image);
        viewContainer.addChild(Bitmap);
        viewContainer.addEventListener("click", (eventData: any) => {
            viewContainer.removeAllChildren();
            notifyFinish();
        });
        // const aside = new GameAsideCommand(3);
        // pool.add(aside)

    }
}

class CheckStoneCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const image = new Image();
        image.src = "assets/map/map3/stone.png";
        const Bitmap = new renderer.Bitmap(160, 96, image);
        viewContainer.addChild(Bitmap);
        viewContainer.addEventListener("click", (eventData: any) => {
            viewContainer.removeAllChildren();
            notifyFinish();
        });
        // const aside = new GameAsideCommand(3);
        // pool.add(aside)

    }
}


class ChosenCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const image = new Image();
        image.src = "dialog.png";
        const img1 = new Image();
        img1.src = "choosebutton1.png";
        const img2 = new Image();
        img2.src = "choosebutton2.png";
        const img3 = new Image();
        img3.src = "choosebutton3.png";
        const Bitmap = new renderer.Bitmap(30, 30, image);
        const Img1 = new renderer.Bitmap(266, 204, img1);
        const Img2 = new renderer.Bitmap(266, 264, img2);
        const Img3 = new renderer.Bitmap(266, 324, img3);
        // viewContainer.addChild(Bitmap);
        viewContainer.addChild(Img1);
        viewContainer.addChild(Img2);
        viewContainer.addChild(Img3)

        Img1.addEventListener("click", (eventData: any) => {
            mapManager.choose = 1
            viewContainer.removeAllChildren();
            const died = new DiedCommand();
            const aside = new GameAsideCommand(8);
            pool.add(died);
            pool.add(aside);
            notifyFinish();
        });
        Img2.addEventListener("click", (eventData: any) => {
            mapManager.choose = 2
            viewContainer.removeAllChildren();
            const aside = new GameAsideCommand(9);
            const stuff = new StuffCommand();
            pool.add(aside);
            pool.add(stuff);
            notifyFinish();
        });
        Img3.addEventListener("click", (eventData: any) => {
            mapManager.choose = 3
            viewContainer.removeAllChildren();
            notifyFinish();
        });
        // const aside = new GameAsideCommand(3);
        // pool.add(aside)

    }

}

class DiedCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const img = new Image();
        img.src = "死亡界面.png";
        const bitmap = new renderer.Bitmap(0, 0, img)
        viewContainer2.addChild(bitmap);
        bitmap.addEventListener("click", (eventData: any) => {

            notifyFinish();
        });
    }
}

class StuffCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const img = new Image();
        img.src = "普通人结局.png"
        const bitmap = new renderer.Bitmap(0, 0, img)
        const img2 = new Image();
        img2.src = "通关界面.png";
        const bitmap2 = new renderer.Bitmap(0, 0, img2)
        viewContainer2.addChild(bitmap);
        bitmap.addEventListener("click", (eventData: any) => {
            viewContainer2.addChild(bitmap2);
            notifyFinish();
        });
    }
}
class TrueChosenCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const image = new Image();
        image.src = "dialog.png";
        const img1 = new Image();
        img1.src = "choosebutton1.png";
        const img2 = new Image();
        img2.src = "choosebutton2.png";

        const Bitmap = new renderer.Bitmap(30, 30, image);
        const Img1 = new renderer.Bitmap(266, 204, img1);
        const Img2 = new renderer.Bitmap(266, 264, img2);

        // viewContainer.addChild(Bitmap);
        viewContainer.addChild(Img1);
        viewContainer.addChild(Img2);


        Img1.addEventListener("click", (eventData: any) => {
            mapManager.choose = 1
            viewContainer.removeAllChildren();
            const aside = new GameAsideCommand(16);

            pool.add(aside);

            notifyFinish();
        });
        Img2.addEventListener("click", (eventData: any) => {
            mapManager.choose = 2
            viewContainer.removeAllChildren();
            const aside = new GameAsideCommand(9);
            const stuff = new StuffCommand();
            pool.add(aside);
            pool.add(stuff);
            notifyFinish();
        });

    }

}

class TrueEndingCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const aside = new GameAsideCommand(17);
        const ending = new TrueCommand();
        pool.add(aside);
        pool.add(ending);
        notifyFinish();
    }
}
class TrueCommand extends command.Command {
    constructor() {
        super();
    }

    execute(notifyFinish: Function) {
        const img = new Image();
        img.src = "大法师结局.png"
        const bitmap = new renderer.Bitmap(0, 0, img)
        const img2 = new Image();
        img2.src = "通关界面.png";
        const bitmap2 = new renderer.Bitmap(0, 0, img2)
        viewContainer2.addChild(bitmap);
        bitmap.addEventListener("click", (eventData: any) => {
            viewContainer2.addChild(bitmap2);
            notifyFinish();
        });
    }
}